// pages/reward/detail/detail.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    detailId: 'id',
    detailData: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      detailId: options.id
    })
    wx.setStorageSync("userId", "10002624")
    this.getDetailData()
  },

  getDetailData: function () {
    var that = this
    app.fetchPostApi('market/gashapon/detail', { "id": this.data.detailId,"userId":wx.getStorageSync("userId")}).then(data => {
      //判断扭蛋机状态
      var myDate = parseInt(Date.parse(new Date()).toString().substring(0, 10))
      if (data.startTime > myDate) {//未开始
        data.type = 0
      }else {//开始
        if (data.startTime == -1 && data.endTime == -1) {//没有设置时间
          data.type = 1
        } else if (data.endTime == -1) {//判断开始时间或结束时间未设置的情况
          data.type = 1
        } else {
          if (data.endTime < myDate) {//已经结束
            data.type = 3
          } else {
            data.type = 2
          }
        }
      }

      for (var i=0; i<data.gifts.length; i++) {
        var gift = data.gifts[i]
        gift.img = app.globalData.base_image_url + gift.img
        if (gift.remainCount == 0) {//抽完
          gift.isRed = false
          gift.remainStr = "抽完啦"
        } else if (gift.remainCount < 10) {
          gift.isRed = true
          gift.remainStr = "仅剩" + gift.remainCount
        } else if (gift.remainCount > 10 && gift.remainCount <= 99) {
          gift.isRed = false
          gift.remainStr = "剩余" + gift.remainCount
        } else {
          gift.isRed = false
          gift.remainStr = "剩余99+"
        }
      }

      console.log(data)
      that.setData({
        detailData: data
      })
      
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})